package com.seguriapp.dialogs;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import com.seguriapp.R;
import com.seguriapp.util.AppPrefences;
import com.seguriapp.util.Constants;

public class UserInformationDialog extends Dialog {

	public static final int INTERVAL = 100;
	private static UserInformationDialog dialog;
	private static Context mContext;
	private static EditText userName;
	private static EditText userLastName;
	private static EditText userAddress;
	private static EditText userPhone;
	private static EditText userMail;
	private static EditText userDni;
	private static EditText userBirthday;

	private static Calendar dateTime = Calendar.getInstance();

	private UserInformationDialog(final Context context) {
		super(context, android.R.style.Theme_Holo_Light_Dialog);
		mContext = context;
		setContentView(R.layout.dialog_userinformation);
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	public static UserInformationDialog createDialog(final Context context) {
		dialog = new UserInformationDialog(context);

		setUserInformation();
		userBirthday.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				chooseDate(dateTime, d);

			}
		});

		final Button btnReg = (Button) dialog.findViewById(R.id.btnReg);
		btnReg.setVisibility(View.VISIBLE);
		btnReg.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(final View view) {
				registerUserInformation();
			}
		});
		
		return dialog;
	}

	public static void chooseDate(Calendar dateTime,
			DatePickerDialog.OnDateSetListener dateListener) {
		new DatePickerDialog(mContext, dateListener,
				dateTime.get(Calendar.YEAR), dateTime.get(Calendar.MONTH),
				dateTime.get(Calendar.DAY_OF_MONTH)).show();
	}

	static DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			dateTime.set(Calendar.YEAR, year);
			dateTime.set(Calendar.MONTH, monthOfYear);
			dateTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			updateDate(dateTime, R.id.txtFechaNacimiento);
		}
	};

	private static void updateDate(final Calendar dateTime, final int view) {
		EditText txtEmail = (EditText) dialog.findViewById(view);
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
		txtEmail.setText(sdf.format(dateTime.getTime()));
	}

	/**
	 * 
	 */
	public static void setUserInformation() {
		userName = (EditText) dialog.findViewById(R.id.name);
		userLastName = (EditText) dialog.findViewById(R.id.lastname);
		userAddress = (EditText) dialog.findViewById(R.id.address);
		userPhone = (EditText) dialog.findViewById(R.id.phone);
		userMail = (EditText) dialog.findViewById(R.id.mail);
		userDni = (EditText) dialog.findViewById(R.id.dni);
		userBirthday = (EditText) dialog.findViewById(R.id.txtFechaNacimiento);

		final AppPrefences msp = new AppPrefences(mContext);
		userName.setText(msp.getUserName());
		userLastName.setText(msp.getUserLastName());
		userAddress.setText(msp.getUserAddress());
		userPhone.setText(msp.getUserPhone());
		userDni.setText(msp.getUserDni());
		userMail.setText(msp.getUserMail());
		userBirthday.setText(msp.getUserBirthday());
	}

	/**
	 * 
	 */
	public static void registerUserInformation() {
		final AppPrefences msp = new AppPrefences(mContext);
		msp.setUserName(userName.getText().toString());
		msp.setUserLastName(userLastName.getText().toString());
		msp.setUserAddress(userAddress.getText().toString());
		msp.setUserPhone(userPhone.getText().toString());
		msp.setUserDni(userDni.getText().toString());
		if (checkEmail(userMail.getText().toString())) {
			msp.setUserMail(userMail.getText().toString());
		}
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy",
				Locale.US);
		msp.setUserBirthday(sdf.format(dateTime.getTime()));
		dialog.dismiss();
	}

	/**
	 * Compare with a pattern
	 */
	private static boolean checkEmail(final String email) {
		return Constants.EMAIL_ADDRESS_PATTERN.matcher(email).matches();
	}
}
